package net.entelijan.cobean.examples.showcase.config;

import java.awt.Container;
import java.net.URL;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import net.entelijan.scf.ShowcaseDescriptor;
import net.miginfocom.swing.MigLayout;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CobeanConfigShowcaseRunner {

	public CobeanConfigShowcaseRunner() {
		super();
	}

	public static void main(String[] args) {
		final JFrame frame = new JFrame("Cobean Config Showcase");
		URL url = CobeanConfigShowcaseRunner.class.getClassLoader()
				.getResource("logo-small-white.png");
		if (url != null) {
			frame.setIconImage(new ImageIcon(url).getImage());
		}
		Container cp = frame.getContentPane();
		cp.setLayout(new MigLayout());
		CobeanConfigShowcasePanel topPanel = new CobeanConfigShowcasePanel();
		for (ShowcaseDescriptor d : loadShowcaseDescs()) {
			d.init(topPanel);
		}
		cp.add(topPanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 800);
		frame.setLocation(100, 200);
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				frame.setVisible(true);
			}

		});
	}

	@SuppressWarnings("unchecked")
	private static Collection<ShowcaseDescriptor> loadShowcaseDescs() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"config-showcase.xml");
		return (Collection<ShowcaseDescriptor>) ctx.getBeansOfType(
				ShowcaseDescriptor.class).values();
	}

}
